package org.zhadoop.streaming.util.datatype;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.streaming.exception.StreamingException;

import com.google.common.base.Strings;

public class LongParser implements DataTypeParser{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7576720456278742672L;

	private static final Logger LOG = LoggerFactory.getLogger(LongParser.class);
	
	@Override
	public Object createValue(String value) throws StreamingException {
		if(Strings.isNullOrEmpty(value)){
			return null;
		}
		try{
			return Long.valueOf(value);
		}catch(NumberFormatException e){
			  LOG.warn("Failed to create {} instance.", Long.class.getName());
	          throw new StreamingException("Failed to create " + Long.class.getName() + " instance.");
		}
	}

	@Override
	public String toStringValue(Object value) throws StreamingException {
		if (null == value)
        {
            return null;
        }
        return value.toString();
	}
}
